<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="什么是Makefile我们在进行实际开发的时候工程里面可能有几十上百个文件等待编译，为解决这个问题。用来描述哪些文件需要编译、哪些需要重新编译的文件就叫做Makefile。 Makefile功能 如果工程没有编译过，那么工程中的所有 .c文件都要被编译并且链接成可执行程序。 如果工程中只有个别 C文件被修改了，那么只 编译这些被修改的 C文件即可。 如果工程的头文件被修改了，那么我们需要编译所有引">
<meta name="keywords" content="STM32,c++,Java,电子设计,Linux">
<meta property="og:type" content="article">
<meta property="og:title" content="makefile基础">
<meta property="og:url" content="https://hxk0912.github.io/2020/01/29/makefile基础/index.html">
<meta property="og:site_name" content="侯学凯的博客">
<meta property="og:description" content="什么是Makefile我们在进行实际开发的时候工程里面可能有几十上百个文件等待编译，为解决这个问题。用来描述哪些文件需要编译、哪些需要重新编译的文件就叫做Makefile。 Makefile功能 如果工程没有编译过，那么工程中的所有 .c文件都要被编译并且链接成可执行程序。 如果工程中只有个别 C文件被修改了，那么只 编译这些被修改的 C文件即可。 如果工程的头文件被修改了，那么我们需要编译所有引">
<meta property="og:locale" content="default">
<meta property="og:updated_time" content="2020-09-04T03:23:03.382Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="makefile基础">
<meta name="twitter:description" content="什么是Makefile我们在进行实际开发的时候工程里面可能有几十上百个文件等待编译，为解决这个问题。用来描述哪些文件需要编译、哪些需要重新编译的文件就叫做Makefile。 Makefile功能 如果工程没有编译过，那么工程中的所有 .c文件都要被编译并且链接成可执行程序。 如果工程中只有个别 C文件被修改了，那么只 编译这些被修改的 C文件即可。 如果工程的头文件被修改了，那么我们需要编译所有引">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hxk0912.github.io/2020/01/29/makefile基础/">





  <title>makefile基础 | 侯学凯的博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="default">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">侯学凯的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">一个大学生的技术博客</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>

<script>
    
    window.onload = function(){
        var path = 'https://hxk0912.github.io'; //这里要改成你博客的地址
        var localhostItem = String(window.location).split(path)[1];
        var LiNode = document.querySelectorAll('#menu > li > a')
        
        for(var i = 0; i< LiNode.length;i++){
            var item = String(LiNode[i].href).split(path)[1];
            if(item == localhostItem && item != undefined){
                LiNode[i].setAttribute('style','border-bottom:1px solid black');
            }
        }
    };

</script>


 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://hxk0912.github.io/2020/01/29/makefile基础/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Kai">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="侯学凯的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">makefile基础</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2020-01-29T16:39:56+08:00">
                2020-01-29
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Linux/" itemprop="url" rel="index">
                    <span itemprop="name">Linux</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="什么是Makefile"><a href="#什么是Makefile" class="headerlink" title="什么是Makefile"></a>什么是Makefile</h2><p>我们在进行实际开发的时候工程里面可能有几十上百个文件等待编译，为解决这个问题。用来描述哪些文件需要编译、哪些需要重新编译的文件就叫做Makefile。</p>
<h2 id="Makefile功能"><a href="#Makefile功能" class="headerlink" title="Makefile功能"></a>Makefile功能</h2><ol>
<li>如果工程没有编译过，那么工程中的所有 .c文件都要被编译并且链接成可执行程序。</li>
<li>如果工程中只有个别 C文件被修改了，那么只 编译这些被修改的 C文件即可。</li>
<li>如果工程的头文件被修改了，那么我们需要编译所有引用这个头文件的 C文件，并且链接成可执行文件。</li>
</ol>
<h2 id="Makefile语法"><a href="#Makefile语法" class="headerlink" title="Makefile语法"></a>Makefile语法</h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">main:main.o a.o b.o</span></span><br><span class="line">    gcc -o main main.o a.o b.o</span><br><span class="line"><span class="section">main.o:main.c</span></span><br><span class="line">    gcc -c main.c</span><br><span class="line"><span class="section">a.o:a.c</span></span><br><span class="line">    gcc -c a.c</span><br><span class="line"><span class="section">b.o:b.c</span></span><br><span class="line">    gcc -c b.c</span><br><span class="line"></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">    rm *.o</span><br><span class="line">    rm main</span><br></pre></td></tr></table></figure>
<p><strong>命令列表中的每条命令必须以TAB键开始，不能使用空格！</strong></p>
<p>简单总结：<br>[目标]：[依赖]<br>    [命令]</p>
<h3 id="执行过程"><a href="#执行过程" class="headerlink" title="执行过程"></a>执行过程</h3><ol>
<li>make命令会在当前目录下查找以 Makefile(makefile其实也可以 )命名的文件。</li>
<li>当找到 Makefile文件以后就会按照 Makefile中定义的规则去编译生成最终的目标文件。</li>
<li>当发现目标文件不存在，或者目标所依赖的文件比目标文件新 (也就是最后修改时间比目标文件晚 )的话就会执行后面的命令来更新目标。</li>
</ol>
<h2 id="伪目标"><a href="#伪目标" class="headerlink" title="伪目标"></a>伪目标</h2><p>比如我们文件夹中有了一个名叫 clean的文件，那么每次都是最新的文件，就不会正常执行我们定义的命令了。</p>
<p>为了解决这种问题，Makefile使用”.PHONY”前缀来区分目标代号和目标文件，并且这种目标代号被称为”伪目标”。</p>
<p>也就是说如果不需要生成文件，就把目标定义为伪目标就好</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta"><span class="meta-keyword">.PHONY</span>:clean</span></span><br><span class="line"><span class="section">clean: </span></span><br><span class="line">    rm -f *.o</span><br></pre></td></tr></table></figure>
<h2 id="默认规则"><a href="#默认规则" class="headerlink" title="默认规则"></a>默认规则</h2><p>当找不到xxx. o文件时，会查找目录下的同名xxx.c文件进行编译。</p>
<p>比如我需要a.o而且我文件夹内有a.c，那么我就可以省略编译a.c编程a.o的代码。</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#下面两行根据默认规则可以省略了</span></span><br><span class="line"><span class="comment">#a.o:a.c</span></span><br><span class="line"><span class="comment">#    gcc -c a.c</span></span><br></pre></td></tr></table></figure>
<h2 id="Makefile变量"><a href="#Makefile变量" class="headerlink" title="Makefile变量"></a>Makefile变量</h2><p>makefile中变量就类似于C语言中的宏定义</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#   makefile 变量的使用</span></span><br><span class="line">macro = main.o a.o b.o</span><br><span class="line"><span class="section">main:<span class="variable">$(macro)</span></span></span><br><span class="line">    gcc -o main <span class="variable">$(macro)</span></span><br></pre></td></tr></table></figure>
<h3 id="赋值符"><a href="#赋值符" class="headerlink" title="赋值符"></a>赋值符</h3><ul>
<li>=  延时赋值，该变量只有在调用的时候，才会被赋值,变量赋值后会一直使用最新的变量值</li>
<li>:= 直接赋值，与延时赋值相反，使用直接赋值的话，变量的值定义时就已经确定了,变量赋值后只会使用最初定义的变量值</li>
<li>?= 若变量的值为空，则进行赋值，通常用于设置默认值。,变量赋值时如果前面有定义就使用前面的值，没有定义就用这个值</li>
<li>+= 追加赋值，可以往变量后面增加新的内容,就像字符串的拼接</li>
</ul>
<h3 id="改造默认规则"><a href="#改造默认规则" class="headerlink" title="改造默认规则"></a>改造默认规则</h3><p>在默认生成规则中，并不包括头文件的依赖，我们如果想加入头文件的依赖，可以使用”%”的通配符和变量来改造规则</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">HEAD = a.h</span><br><span class="line"></span><br><span class="line"><span class="section">%.o:%.c <span class="variable">$(HEAD)</span></span></span><br></pre></td></tr></table></figure>
<p>%.o代表了所有以.o结尾的文件，所以这个命令就是相当于在默认规则的基础上加上了头文件的依赖。</p>
<h3 id="改造链接规则"><a href="#改造链接规则" class="headerlink" title="改造链接规则"></a>改造链接规则</h3><p>我们可以利用变量和自动化变量来使得链接过程更加的简洁和通用</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#定义变量</span></span><br><span class="line">TARGET = main</span><br><span class="line">CC = gcc</span><br><span class="line">HEAD = a.h</span><br><span class="line">OBJS = main.o main.h</span><br><span class="line"></span><br><span class="line"><span class="comment">#目标文件</span></span><br><span class="line"><span class="variable">$(TARGET)</span>: <span class="variable">$(OBJS)</span></span><br><span class="line"> <span class="variable">$(CC)</span> -o <span class="variable">$@</span> <span class="variable">$^</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#*.o文件的生成规则</span></span><br><span class="line"><span class="section">%.o: %.c <span class="variable">$(HEAD)</span></span></span><br><span class="line"><span class="variable">$(CC)</span> -c -o <span class="variable">$@</span> <span class="variable">$&lt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#伪目标</span></span><br><span class="line"><span class="meta"><span class="meta-keyword">.PHONY</span>: clean</span></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">rm -f \*.o main</span><br></pre></td></tr></table></figure>
<p>经过改造后可以明显看出来整个过程变得很简洁，只需要修改变量就可以改变整个生成过程，使得整个过程很通用。</p>
<h3 id="自动化变量"><a href="#自动化变量" class="headerlink" title="自动化变量"></a>自动化变量</h3><div class="table-container">
<table>
<thead>
<tr>
<th>符号</th>
<th>意义</th>
</tr>
</thead>
<tbody>
<tr>
<td>$@</td>
<td>匹配目标文件</td>
</tr>
<tr>
<td>$%</td>
<td>与$@类似，但$%仅匹配”库”类型的目标文件</td>
</tr>
<tr>
<td>$&lt;</td>
<td>依赖中的第一个目标文件</td>
</tr>
<tr>
<td>$^</td>
<td>所有的依赖目标，如果依赖中有重复的，只保留一份</td>
</tr>
<tr>
<td>$+</td>
<td>所有的依赖目标，即使依赖中有重复的也原样保留</td>
</tr>
<tr>
<td>$?</td>
<td>所有比目标要新的依赖目标</td>
</tr>
</tbody>
</table>
</div>
<h2 id="使用分支"><a href="#使用分支" class="headerlink" title="使用分支"></a>使用分支</h2><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ifeq</span>(arg1, arg2)</span><br><span class="line">分支1</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">分支2</span><br><span class="line"><span class="keyword">endif</span></span><br></pre></td></tr></table></figure>
<p>常用于修改编译器</p>
<figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#根据输入的ARCH变量来选择编译器</span></span><br><span class="line"><span class="comment">#ARCH=x86，使用gcc</span></span><br><span class="line"><span class="comment">#ARCH=arm，使用arm-gcc</span></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(ARCH)</span>,x86)</span><br><span class="line">CC = gcc</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">CC = arm-linux-gnueabihf-gcc</span><br><span class="line"><span class="keyword">endif</span></span><br></pre></td></tr></table></figure>
<p>同时可以在make命令时修改ARCH来更改编译器<code>make ARCH=arm</code>,这样就是使用arm-gcc</p>
<h2 id="使用函数"><a href="#使用函数" class="headerlink" title="使用函数"></a>使用函数</h2><p>在实际工程中，头文件、源文件可能会放在二级目录，编译生成的*.o或可执行文件也放到专门的编译输出目录方便整理，实现这些功能就要使用Makefile函数。</p>
<h3 id="函数使用格式"><a href="#函数使用格式" class="headerlink" title="函数使用格式"></a>函数使用格式</h3><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$(函数名 参数)</span><br><span class="line"><span class="comment">#或者使用花括号</span></span><br><span class="line">$&#123;函数名 参数&#125;</span><br></pre></td></tr></table></figure>
<h3 id="常用函数"><a href="#常用函数" class="headerlink" title="常用函数"></a>常用函数</h3><h4 id="notdir函数"><a href="#notdir函数" class="headerlink" title="notdir函数"></a>notdir函数</h4><p>notdir函数用于去除文件路径中的目录部分。它的格式如下：<code>$(notdir 文件名)</code></p>
<p>例如输入参数”./sources/a.c”，函数执行后 的输出为”a.c”，也就是说它会把输入中的”./sources/”路径部分去掉，保留文件名。</p>
<h4 id="wildcard函数"><a href="#wildcard函数" class="headerlink" title="wildcard函数"></a>wildcard函数</h4><p>wildcard函数用于获取文件列表，并使用空格分隔开。它的格式如下：<code>$(wildcard 匹配规则)</code></p>
<p>例如函数调用”$(wildcard *.c)”，函数执行后会把当前目录的所 有c文件列出。</p>
<h4 id="patsubst函数"><a href="#patsubst函数" class="headerlink" title="patsubst函数"></a>patsubst函数</h4><p>patsubst函数功能为模式字符串替换。它的格式如下：<code>$(patsubst 匹配规则, 替换规则, 输入的字符串)</code><br>当输入的字符串符合匹配规则，那么使用替换规则来替换字符串，当匹配规则中有”%”号时，替换规则也可以例程”%”号来提取”%”匹配的内容加入到最后替换的字符串中。</p>
<h3 id="多级结构工程的Makefile"><a href="#多级结构工程的Makefile" class="headerlink" title="多级结构工程的Makefile"></a>多级结构工程的Makefile</h3><figure class="highlight makefile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#定义变量</span></span><br><span class="line"><span class="comment">#ARCH默认为x86，使用gcc编译器，</span></span><br><span class="line"><span class="comment">#否则使用arm编译器</span></span><br><span class="line">ARCH ?= x86</span><br><span class="line">TARGET = hello_main</span><br><span class="line"></span><br><span class="line"><span class="comment">#存放中间文件的路径</span></span><br><span class="line">BUILD_DIR = build_<span class="variable">$(ARCH)</span></span><br><span class="line"><span class="comment">#存放源文件的文件夹</span></span><br><span class="line">SRC_DIR = sources</span><br><span class="line"><span class="comment">#存放头文件的文件夹</span></span><br><span class="line">INC_DIR = includes .</span><br><span class="line"></span><br><span class="line"><span class="comment">#源文件</span></span><br><span class="line">SRCS = <span class="variable">$(<span class="built_in">wildcard</span> <span class="variable">$(SRC_DIR)</span>/*.c)</span></span><br><span class="line"><span class="comment">#目标文件（*.o）</span></span><br><span class="line">OBJS = <span class="variable">$(<span class="built_in">patsubst</span> %.c, <span class="variable">$(BUILD_DIR)</span>/%.o, $(<span class="built_in">notdir</span> <span class="variable">$(SRCS)</span>)</span>)</span><br><span class="line"><span class="comment">#头文件</span></span><br><span class="line">DEPS = <span class="variable">$(<span class="built_in">wildcard</span> <span class="variable">$(INC_DIR)</span>/*.h)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#指定头文件的路径</span></span><br><span class="line">CFLAGS = <span class="variable">$(<span class="built_in">patsubst</span> %, -I%, <span class="variable">$(INC_DIR)</span>)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#根据输入的ARCH变量来选择编译器</span></span><br><span class="line"><span class="comment">#ARCH=x86，使用gcc</span></span><br><span class="line"><span class="comment">#ARCH=arm，使用arm-gcc</span></span><br><span class="line"><span class="keyword">ifeq</span> (<span class="variable">$(ARCH)</span>,x86)</span><br><span class="line">CC = gcc</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">CC = arm-linux-gnueabihf-gcc</span><br><span class="line"><span class="keyword">endif</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#目标文件</span></span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>/<span class="variable">$(TARGET)</span>: <span class="variable">$(OBJS)</span></span><br><span class="line"><span class="variable">$(CC)</span> -o <span class="variable">$@</span> <span class="variable">$^</span> <span class="variable">$(CFLAGS)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#*.o文件的生成规则</span></span><br><span class="line"><span class="variable">$(BUILD_DIR)</span>/%.o: <span class="variable">$(SRC_DIR)</span>/%.c <span class="variable">$(DEPS)</span></span><br><span class="line"><span class="comment">#创建一个编译目录，用于存放过程文件</span></span><br><span class="line"><span class="comment">#命令前带"@",表示不在终端上输出</span></span><br><span class="line">@mkdir -p <span class="variable">$(BUILD_DIR)</span></span><br><span class="line"><span class="variable">$(CC)</span> -c -o <span class="variable">$@</span> <span class="variable">$&lt;</span> <span class="variable">$(CFLAGS)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#伪目标</span></span><br><span class="line"><span class="meta"><span class="meta-keyword">.PHONY</span>: clean cleanall</span></span><br><span class="line"><span class="comment">#按架构删除</span></span><br><span class="line"><span class="section">clean:</span></span><br><span class="line">rm -rf <span class="variable">$(BUILD_DIR)</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#全部删除</span></span><br><span class="line"><span class="section">cleanall:</span></span><br><span class="line">rm -rf build_x86 build_arm</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/01/29/Linux-C的Hello-world/" rel="next" title="Linux-C的Hello-world">
                <i class="fa fa-chevron-left"></i> Linux-C的Hello-world
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/02/14/Linux中文件类型和用户权限管理/" rel="prev" title="Linux中文件类型和用户权限管理">
                Linux中文件类型和用户权限管理 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Kai</p>
              <p class="site-description motion-element" itemprop="description">这里我会放一些平常学习积累的东西，我的邮箱是hxk0912@foxmail.com</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">129</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">38</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#什么是Makefile"><span class="nav-number">1.</span> <span class="nav-text">什么是Makefile</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Makefile功能"><span class="nav-number">2.</span> <span class="nav-text">Makefile功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Makefile语法"><span class="nav-number">3.</span> <span class="nav-text">Makefile语法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#执行过程"><span class="nav-number">3.1.</span> <span class="nav-text">执行过程</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#伪目标"><span class="nav-number">4.</span> <span class="nav-text">伪目标</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#默认规则"><span class="nav-number">5.</span> <span class="nav-text">默认规则</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Makefile变量"><span class="nav-number">6.</span> <span class="nav-text">Makefile变量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#赋值符"><span class="nav-number">6.1.</span> <span class="nav-text">赋值符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#改造默认规则"><span class="nav-number">6.2.</span> <span class="nav-text">改造默认规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#改造链接规则"><span class="nav-number">6.3.</span> <span class="nav-text">改造链接规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自动化变量"><span class="nav-number">6.4.</span> <span class="nav-text">自动化变量</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用分支"><span class="nav-number">7.</span> <span class="nav-text">使用分支</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#使用函数"><span class="nav-number">8.</span> <span class="nav-text">使用函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#函数使用格式"><span class="nav-number">8.1.</span> <span class="nav-text">函数使用格式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#常用函数"><span class="nav-number">8.2.</span> <span class="nav-text">常用函数</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#notdir函数"><span class="nav-number">8.2.1.</span> <span class="nav-text">notdir函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#wildcard函数"><span class="nav-number">8.2.2.</span> <span class="nav-text">wildcard函数</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#patsubst函数"><span class="nav-number">8.2.3.</span> <span class="nav-text">patsubst函数</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多级结构工程的Makefile"><span class="nav-number">8.3.</span> <span class="nav-text">多级结构工程的Makefile</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Kai</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
